package com.kehao.newcoder;

public class NC100Solution {
    public int StrToInt (String s) {
        int direction = 1;
        int result =0;
        int oldResult = 0;
        if(s==null) return 0;
        s = s.trim();
        if(s.isEmpty()) return 0;
        char[] charArray = s.toCharArray();
        int i = 0;
        if(charArray[0]=='+'){
            direction =1;
            i++;
        }else if(charArray[0]=='-'){
            direction = -1;
            i++;
        }
        if(s.startsWith("-2147483648")) return -2147483648;
        while (i<charArray.length&&charArray[i]<='9'&&charArray[i]>='0'){
            result = result*10+charArray[i]-'0';
            if(result<0){
                if(direction==1){
                    return Integer.MAX_VALUE;
                }else {
                    return Integer.MIN_VALUE;
                }
            }
            i++;
        }
        return result*direction;
    }
}
